home *** CD-ROM | disk | FTP | other *** search
/ AGA Toolkit '97 / The AGA Toolkit '97.iso / programming / debug / calls / getopt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-07  |  1.4 KB  |  66 lines

  1. /* @(#)getopt.c */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "getopt.h"
  6.  
  7. /*
  8.  * get option letter from argument vector
  9.  */
  10. int
  11.     optind = 1,        /* index into parent argv vector */
  12.     optopt;            /* character checked for validity */
  13. char    *optarg;        /* argument associated with option */
  14.  
  15. int
  16. getopt(nargc, nargv, ostr)
  17. int nargc;
  18. char **nargv, *ostr;
  19. {
  20.     register char    *oli;        /* option letter list index */
  21.     static char    *place = EMSG;    /* option letter processing */
  22.  
  23.     if(!*place) {            /* update scanning pointer */
  24.         if(optind >= nargc || *(place = nargv[optind]) != '-' || !*++place) return(EOF);
  25.         if (*place == '-') {    /* found "--" */
  26.             ++optind;
  27.             return EOF;
  28.         }
  29.     }                /* option letter okay? */
  30.     if ((optopt = (int)*place++) == (int)':' || !(oli = index(ostr,optopt))) {
  31.         if(!*place) ++optind;
  32.         tell(": illegal option -- ");
  33.     }
  34.     if (*++oli != ':') {        /* don't need argument */
  35.         optarg = NULL;
  36.         if (!*place)
  37.             ++optind;
  38.     } else {                /* need an argument */
  39.         if (*place) {            /* no white space */
  40.             optarg = place;
  41.         } else if (nargc <= ++optind) {    /* no arg */
  42.             place = EMSG;
  43.             tell(": option requires an argument -- ");
  44.         } else {
  45.             optarg = nargv[optind];    /* white space */
  46.         }
  47.         place = EMSG;
  48.         ++optind;
  49.     }
  50.     return optopt;            /* dump back option letter */
  51. }
  52.  
  53. int
  54. getarg(nargc, nargv)
  55. int nargc;
  56. char **nargv;
  57. {
  58.     if (nargc <= optind) {
  59.         optarg = (char *) 0;
  60.         return EOF;
  61.     } else {
  62.         optarg = nargv[optind++];
  63.         return 0;
  64.     }
  65. }
  66.